package id.co.shuba.spamobile.activities;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;

import org.json.JSONArray;
import org.json.JSONObject;

import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.services.WebServiceHelper;
import id.co.shuba.spamobile.util.Base64;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AboutActivity extends Activity implements OnClickListener {
	TextView tv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);

		tv = (TextView) findViewById(R.id.txtName);
		Button b = (Button) findViewById(R.id.btnSave);
		b.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_about, menu);
		return true;
	}

	public void onClick(View v) {
		new DownloadTask().execute();
	}

	private class DownloadTask extends AsyncTask<String, Integer, Void> {

		@Override
		protected Void doInBackground(String... params) {
			/*
			WebServiceHelper helper = new WebServiceHelper();
			String result = helper.Call().toString();
			try {								
				final String decom = new String(
						Base64.decode(result, Base64.GZIP));				
				tv.post(new Runnable() {

					public void run() {
						StringBuilder sb = new StringBuilder();
						try {
						      JSONArray jsonArray = new JSONArray(decom);						      						      
						      for (int i = 0; i < jsonArray.length(); i++) {
						        JSONObject jsonObject = jsonArray.getJSONObject(i);
						        sb.append("Value = "+ jsonObject.getString("value") + "\n");						        
						      }
						      tv.setText(decom);
						    } catch (Exception e) {
						      e.printStackTrace();
						    }
						
					}
				});				
			} catch (IOException e) {
				e.printStackTrace();
			}	
			*/
			return null;
		}		
	}

	public static byte[] decompress(byte[] contentBytes)
			throws DataFormatException, IOException {
		
		GZIPInputStream gzipInputStream = null;
		gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(
				contentBytes));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];

		int len;
		while ((len = gzipInputStream.read(buf)) > 0) {
			out.write(buf, 0, len);
		}

		gzipInputStream.close();
		out.close();
		return out.toByteArray();
	}
}
